package org.ymh.unix_file_system.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.ymh.unix_file_system.domain.*;

/**
 * @author lenovo
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 0:31
 */
@Repository
public class DirNodeOperate {
@Autowired
private FileSystemError error;
@Autowired
private GlobalVariable globalVariable;
@Autowired
private JsonDataOperate jsonDataOperate;
@Autowired
private  InodeOperate inodeOperate;
    public DirItem findFileItem(DirNode dirNode,String name)
    {
        for (int i = 0; i < dirNode.childFile.size(); i++) {
            if (dirNode.childFile.get(i).getFileName().equals(name)) {
                return dirNode.childFile.get(i);
            }}
        return null;

    }
    public boolean deleteFileItem(String name)
    {
        for (int i = 0; i < globalVariable.visitingRecord.peek().dirNode.childFile.size(); i++) {
            if (globalVariable.visitingRecord.peek().dirNode.childFile.get(i).getFileName().equals(name)) {
                DirItem dirItem = globalVariable.visitingRecord.peek().dirNode.childFile.get(i);
                INode iNode = globalVariable.inodeArray[dirItem.inodeNumber];
                if(iNode.type==0) {//是文件，直接删
                    //删除节点
                    inodeOperate.inodeDelete(dirItem.inodeNumber);
                    globalVariable.visitingRecord.peek().dirNode.childFile.remove(i);//删除文件项
                }
                else if(iNode.type==1)//是目录
                {
                    DirNode dirNodeByNumber = getDirNodeByNumber(dirItem.inodeNumber);
                    if(dirNodeByNumber!=null&&dirNodeByNumber.childFile.size()==0)//空目录，可以删
                    {
                        inodeOperate.inodeDelete(dirItem.inodeNumber);
                        globalVariable.visitingRecord.peek().dirNode.childFile.remove(i);//删除文件项
                    }
                    else{
                        //
                        error.setError(error.DeleteDirNodeHaveItemError);//置错
                        return false;
                    }
                }
            }}
        return false;

    }
    public boolean addFileItem(DirItem dirItem)
    {
        if(!globalVariable.visitingRecord.peek().dirNode.childFile.isEmpty()) {
            for (int i = 0; i < globalVariable.visitingRecord.peek().dirNode.childFile.size(); i++) {
                if (globalVariable.visitingRecord.peek().dirNode.childFile.get(i).fileName.equals(dirItem.getFileName())) {
                    error.setError(error.SameFileNameError);
                    return false;
                }
            }
        }
        globalVariable.visitingRecord.peek().dirNode.childFile.add(dirItem);
        return true;
    }
    public DirNode getDirNodeByNumber(Integer number)
    {
        INode iNode = globalVariable.inodeArray[number];
        if(iNode.type==1)
        {
            return (DirNode) jsonDataOperate.jsonFileParse(new DirNode(),iNode.getAddress()[0]+"");
        }
        return null;
    }

}
